package com.warren.biz.service.impl;

import com.warren.biz.entity.Health;
import com.warren.biz.mapper.HealthMapper;
import com.warren.biz.service.HealthService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.warren.handler.BusinessException;
import com.warren.response.ResultCode;
import com.warren.biz.vo.HealthVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author warren
 * @since 2021-04-20
 */
@Service
public class HealthServiceImpl extends ServiceImpl<HealthMapper, Health> implements HealthService{

    @Override
    public void report(HealthVO healthVO) throws BusinessException {
        Health report = isReport(healthVO.getUserId());
        if (report != null){
            throw new BusinessException(ResultCode.HASH_REPORT.getCode(), ResultCode.HASH_REPORT.getMessage());
        }
        Health health = new Health();
        BeanUtils.copyProperties(healthVO,health);
        this.baseMapper.insert(health);
    }

    @Override
    public Health isReport(Long id) {
        List<Health> health = this.baseMapper.isReport(id);
        if (health.size() > 0){
            return health.get(0);
        }
        return null;
    }
}
